package service;

import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.jms.TextMessage;

import newspaper.VnexpressContent;
import newspaper.VnexpressThreadManager;


public class VnexpressMessageReceiver implements ServletContextListener,
		MessageListener, ExceptionListener {

	@Override
	public void onException(JMSException arg0) {
		throw new UnsupportedOperationException("Not supported yet.");

	}

	@Override
	public void onMessage(Message message) {		
		System.out.print("From queue : ");
		try {
			TextMessage textMessage = (TextMessage) message;
			System.out.print(textMessage.getText());
			VnexpressContent contentLove = new VnexpressContent(textMessage.getText()); 
			VnexpressThreadManager.listContentThread.add(contentLove);
			contentLove.start();
		} catch (JMSException e) {			
			e.printStackTrace();
		}
		System.out.println(", get ok !"); 
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		throw new UnsupportedOperationException("Not supported yet.");

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		throw new UnsupportedOperationException("Not supported yet.");

	}
	
	public void getMessage() throws JMSException,
			InterruptedException {		
		ActiveMQService.setUp("contentSMS");
		MessageConsumer consumer = ActiveMQService.session.createConsumer(ActiveMQService.destination);
		ActiveMQService.connection.setExceptionListener(this);
		consumer.setMessageListener(this);
	}			
}
